home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / DCLAP 6d / dclap6d / DNet.more / url.c < prev    next >
Text File  |  1996-07-05  |  13KB  |  407 lines

  1.  
  2. from lynx:
  3.  
  4. /* universal document id types */
  5. #define HTTP_URL_TYPE     1
  6. #define FILE_URL_TYPE     2
  7. #define FTP_URL_TYPE      3
  8. #define WAIS_URL_TYPE     4
  9. #define PROSPERO_URL_TYPE 5
  10. #define NEWS_URL_TYPE     6
  11. #define TELNET_URL_TYPE   7
  12. #define TN3270_URL_TYPE   8
  13. #define GOPHER_URL_TYPE   9
  14. #define HTML_GOPHER_URL_TYPE 10
  15. #define TELNET_GOPHER_URL_TYPE 11
  16. #define INDEX_GOPHER_URL_TYPE 12
  17. #define AFS_URL_TYPE      13
  18. #define MAILTO_URL_TYPE   14
  19. #define RLOGIN_URL_TYPE   15
  20.  
  21. #define NEWSPOST_URL_TYPE      19
  22.  
  23. // convert to Protocol enums ??
  24. enum    urltypes {
  25.     kUnknownUrl = 0,
  26.     kFileUrl,
  27.     kFtpUrl,
  28.     kTelnetUrl,
  29.     kTN3270Url,
  30.     kGopherUrl,
  31.     kMailtoUrl,
  32.     kHttpUrl,
  33.     kWaisUrl,
  34.     kNewsUrl,
  35.     kNewspostUrl,
  36.     kProsperoUrl,
  37.     kAfsUrl,
  38.     kRloginUrl
  39.     };
  40.     
  41.     
  42. short IsURL(char* name)
  43. {    // from Lynx
  44.     char *cp= name;
  45.  
  46.     /* don't crash on an empty argument */
  47.     if (cp == NULL || *cp == '\0')
  48.         return(kUnknownUrl);
  49.  
  50.     /* kill beginning spaces */
  51.     while(isspace(*cp)) cp++;
  52.  
  53.     if(!strncmp(cp,"news:",5)) {
  54.         return(kNewsUrl);
  55.  
  56.     } else if(!strncmp(cp,"mailto:",7)) {
  57.         return(kMailtoUrl);
  58.  
  59.     } else if(!strncmp(cp,"newspost:",9)) {
  60.         return(kNewspostUrl);
  61.  
  62.         /* if it doesn't contain ":/" then it can't be a url
  63.          * except for the ones above here
  64.          */
  65.     } else if(!strstr(cp+3,":/")) {
  66.         return(kUnknownUrl);
  67.  
  68.     } else if(!strncmp(cp,"http",4)) {
  69.         return(kHttpUrl);
  70.  
  71.     } else if(!strncmp(cp,"file",4)) {
  72.         return(kFileUrl);
  73.  
  74.     } else if(!strncmp(cp,"gopher",6)) {
  75.         return(kGopherUrl);
  76.  
  77.     } else if(!strncmp(cp,"ftp",3)) {
  78.         return(kFtpUrl);
  79.  
  80.     } else if(!strncmp(cp,"wais",4)) {
  81.         return(kWaisUrl);
  82.  
  83.     } else if(!strncmp(cp,"telnet",6)) {
  84.         return(kTelnetUrl);
  85.  
  86.     } else if(!strncmp(cp,"tn3270",6)) {
  87.         return(kTN3270Url);
  88.  
  89.     } else if(!strncmp(cp,"rlogin",6)) {
  90.         return(kRloginUrl);
  91.  
  92.     } else if(!strncmp(cp,"afs",3)) {
  93.         return(kAfsUrl);
  94.  
  95.     } else if(!strncmp(cp,"prospero",8)) {
  96.         return(kProsperoUrl);
  97.  
  98.     } else {
  99.         return(kUnknownUrl);
  100.     }
  101. }
  102.  
  103.  
  104.  
  105. from WWW lib:
  106.  
  107. /*    Parse a Name relative to another name
  108. **    -------------------------------------
  109. **
  110. **    This returns those parts of a name which are given (and requested)
  111. **    substituting bits from the related name where necessary.
  112. **
  113. ** On entry,
  114. **    aName        A filename given
  115. **      relatedName     A name relative to which aName is to be parsed
  116. **      wanted          A mask for the bits which are wanted.
  117. **
  118. ** On exit,
  119. **    returns        A pointer to a malloc'd string which MUST BE FREED
  120. */
  121. #ifdef __STDC__
  122. char * HTParse(const char * aName, const char * relatedName, int wanted)
  123. #else
  124. char * HTParse(aName, relatedName, wanted)
  125.     char * aName;
  126.     char * relatedName;
  127.     int wanted;
  128. #endif
  129.  
  130. {
  131.     char * result = 0;
  132.     char * return_value = 0;
  133.     int len;
  134.     char * name = 0;
  135.     char * rel = 0;
  136.     char * p;
  137.     char * access;
  138.     struct struct_parts given, related;
  139.     
  140.     /* Make working copies of input strings to cut up:
  141.     */
  142.     len = strlen(aName)+strlen(relatedName)+10;
  143.     result=(char *)malloc(len);        /* Lots of space: more than enough */
  144.     if (result == NULL) outofmem(__FILE__, "HTParse");
  145.     
  146.     StrAllocCopy(name, aName);
  147.     StrAllocCopy(rel, relatedName);
  148.     
  149.     scan(name, &given);
  150.     scan(rel,  &related); 
  151.     result[0]=0;        /* Clear string  */
  152.     access = given.access ? given.access : related.access;
  153.     if (wanted & PARSE_ACCESS)
  154.         if (access) {
  155.         strcat(result, access);
  156.         if(wanted & PARSE_PUNCTUATION) strcat(result, ":");
  157.     }
  158.     
  159.     if (given.access && related.access)    /* If different, inherit nothing. */
  160.         if (strcmp(given.access, related.access)!=0) {
  161.         related.host=0;
  162.         related.absolute=0;
  163.         related.relative=0;
  164.         related.anchor=0;
  165.     }
  166.     
  167.     if (wanted & PARSE_HOST)
  168.         if(given.host || related.host) {
  169.         char * tail = result + strlen(result);
  170.         if(wanted & PARSE_PUNCTUATION) strcat(result, "//");
  171.         strcat(result, given.host ? given.host : related.host);
  172. #define CLEAN_URLS
  173. #ifdef CLEAN_URLS
  174.         /* Igno, "HTEscape");
  175.     for(q=result, p=str; *p; p++) {
  176.         unsigned char a = TOASCII(*p);
  177.     if (!ACCEPTABLE(a)) {
  178.         *q++ = HEX_ESCAPE;    /* Means hex commming */
  179.         *q++ = hex[a >> 4];
  180.         *q++ = hex[a & 15];
  181.     }
  182.     else *q++ = *p;
  183.     }
  184.     *q++ = 0;            /* Terminate */
  185.     return result;
  186. }
  187.  
  188.  
  189. /*        Decode %xx escaped characters            HTUnEscape()
  190. **        -----------------------------
  191. **
  192. **    This function takes a pointer to a string in which some
  193. **    characters may have been encoded in %xy form, where xy is
  194. **    the acsii hex code for character 16x+y.
  195. **    The string is converted in place, as it will never grow.
  196. */
  197.  
  198. PRIVATE char from_hex ARGS1(char, c)
  199. {
  200.     return  c >= '0' && c <= '9' ?  c - '0' 
  201.             : c >= 'A' && c <= 'F'? c - 'A' + 10
  202.             : c - 'a' + 10;    /* accept small letters just in case */
  203. }
  204.  
  205. PUBLIC char * HTUnEscape ARGS1( char *, str)
  206. {
  207.     char * p = str;
  208.     char * q = str;
  209.  
  210.     if (!str) {                          /* Just for safety ;-) */
  211.     if (TRACE)
  212.         fprintf(stderr, "HTUnEscape.. Called with NULL argument.\n");
  213.     return "";
  214.     }
  215.     while(*p) {
  216.         if (*p == HEX_ESCAPE) {
  217.         p++;
  218.         if (*p) *q = from_hex(*p++) * 16;
  219.         if (*p) *q = FROMASCII(*q + from_hex(*p++));
  220.         q++;
  221.     } else {
  222.         *q++ = *p++; 
  223.     }
  224.     }
  225.     
  226.     *q++ = 0;
  227.     return str;
  228.     
  229. } /* HTUnEscape */
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237. /********************************************************************
  238.  * lindner
  239.  * 3.7
  240.  * 1994/03/04 17:42:57
  241.  * /home/mudhoney/GopherSrc/CVS/gopher+/object/url.c,v
  242.  * Exp
  243.  *
  244.  * Paul Lindner, University of Minnesota CIS.
  245.  *
  246.  * Copyright 1991, 1992 by the Regents of the University of Minnesota
  247.  * see the file "Copyright" in the distribution for conditions of use.
  248.  *********************************************************************
  249.  * MODULE: url.c
  250.  * Simplified method of getting urls..
  251.  *********************************************************************
  252.  * Revision History:
  253.  * url.c,v
  254.  * Revision 3.7  1994/03/04  17:42:57  lindner
  255.  * Fixes from Alan Coopersmith
  256.  *
  257.  * Revision 3.6  1994/01/21  04:25:41  lindner
  258.  * Add support for tn3270 and better gopher url handling
  259.  *
  260.  * Revision 3.5  1993/12/30  04:18:08  lindner
  261.  * translate telnet url correctly
  262.  *
  263.  * Revision 3.4  1993/12/27  16:14:03  lindner
  264.  * Enlarge buffer size, make html docs on gopher server into http: refs
  265.  *
  266.  * Revision 3.3  1993/11/02  06:14:09  lindner
  267.  * Add url html hack
  268.  *
  269.  *
  270.  *********************************************************************/
  271.  
  272. #include "url.h"
  273. #include "GSgopherobj.h"
  274. #include "Malloc.h"
  275. #include "String.h"
  276.  
  277. Url *
  278. URLnew()
  279. {
  280.      Url *temp;
  281.  
  282.      temp = (Url *) malloc(sizeof(Url));
  283.      temp->url = STRnew();
  284.      
  285.      return(temp);
  286. }
  287.  
  288.  
  289. void
  290. URLdestroy(url)
  291.   Url *url;
  292. {
  293.      STRdestroy(url->url);
  294.      free(url);
  295. }
  296.  
  297. /*
  298.  * Make a default url from the current Gopher Object...
  299.  */
  300.  
  301. void
  302. URLfromGS(url, gs)
  303.   Url       *url;
  304.   GopherObj *gs;
  305. {
  306.      char u[2048], *path, *cp;
  307.  
  308.      *u = '\0';
  309.      path  = GSgetPath(gs);
  310.  
  311.      if (path == NULL)
  312.       return;
  313.  
  314.      if (GSgetType(gs) == A_TELNET) {
  315.       if (*path == '\0')
  316.            sprintf(u, "telnet://%s:%d", GSgetHost(gs), GSgetPort(gs));
  317.       else
  318.            sprintf(u, "telnet://%s@%s:%d", path, GSgetHost(gs),
  319.                GSgetPort(gs));
  320.      } else if (GSgetType(gs) == A_TN3270) {
  321.       if (*path == '\0')
  322.            sprintf(u, "tn3270://%s:%d", GSgetHost(gs), GSgetPort(gs));
  323.       else
  324.            sprintf(u, "tn3270://%s@%s:%d", path, GSgetHost(gs),
  325.                GSgetPort(gs));
  326.      } else if (strncmp(path, "GET /", 5) == 0) {
  327.       sprintf(u, "http://%s:%d/%s", GSgetHost(gs), GSgetPort(gs),
  328.           path+5);
  329.      }
  330.      else {
  331.       sprintf(u, "gopher://%s:%d/%c", GSgetHost(gs), GSgetPort(gs),
  332.           GSgetType(gs));
  333.       cp = u + strlen(u);
  334.       Tohexstr(path, cp);
  335.      }
  336.  
  337.      URLset(url, u);
  338. }
  339.  
  340. /*
  341.  * Hack gopher directories into an HTML type...
  342.  */
  343.  
  344. void
  345. URLmakeHTML(url)
  346.   Url *url;
  347. {
  348.      char *cp = URLget(url);
  349.      char *host;
  350.  
  351.      if (cp == NULL) 
  352.       return;
  353.  
  354.      if (strncmp(cp, "gopher://", 9) != 0)
  355.       return;
  356.  
  357.      /** find the type character **/
  358.      cp = strchr(cp+10, '/');
  359.      
  360.      if (cp ==NULL)
  361.       return;
  362.  
  363.      host = cp+10;
  364.      
  365.      /** Test link for current host **/
  366. /*     if (strcasecmp(host, hostname) != 0) 
  367.       return;*/
  368.      
  369.      cp ++;
  370.      /** cp is now pointed at the type character **/
  371.      
  372.      if (*cp == '1' && *(cp+1) == '1') {
  373.       /** It's a directory **/
  374.       *cp = 'h';
  375.       *(cp+1) = 'h';
  376.      }
  377. }
  378.  
  379.  
  380. /*
  381.  * Get the transport of the specified URL
  382.  */
  383.  
  384. char *
  385. URLgetTransport(url)
  386.   Url *url;
  387. {
  388.      static char trans[16];  /** Shouldn't need more than that.. yet.. */
  389.      char *urlcp, *cp;
  390.  
  391.      urlcp = URLget(url);
  392.      if (urlcp == NULL)
  393.       return(NULL);
  394.  
  395.      cp = strchr(urlcp, ':');
  396.      if (cp == NULL)
  397.       return(NULL);
  398.      
  399.      strncpy(trans, urlcp, (int)(cp-urlcp));
  400.      return (trans);
  401. }
  402.      
  403.      
  404.      
  405.  
  406.  
  407.